package com.ruoyi.project.poem.goodsarticle.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.poem.goodsarticle.domain.GoodsArticle;
import com.ruoyi.project.poem.goodsarticle.service.IGoodsArticleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 好物推文Controller
 *
 * @author shixiangwen
 * @date 2021-06-24
 */
@Controller
@RequestMapping("/poem/goodsarticle")
public class GoodsArticleController extends BaseController {
    private String prefix = "poem/goodsarticle";

    @Autowired
    private IGoodsArticleService goodsArticleService;

    @RequiresPermissions("poem:goodsarticle:view")
    @GetMapping()
    public String goodsarticle() {
        return prefix + "/goodsarticle";
    }

    /**
     * 查询好物推文列表
     */
    @RequiresPermissions("poem:goodsarticle:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GoodsArticle goodsArticle) {
        startPage();
        List<GoodsArticle> list = goodsArticleService.selectGoodsArticleList(goodsArticle);
        return getDataTable(list);
    }

    /**
     * 导出好物推文列表
     */
    @RequiresPermissions("poem:goodsarticle:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GoodsArticle goodsArticle) {
        List<GoodsArticle> list = goodsArticleService.selectGoodsArticleList(goodsArticle);
        ExcelUtil<GoodsArticle> util = new ExcelUtil<GoodsArticle>(GoodsArticle.class);
        return util.exportExcel(list, "goodsarticle");
    }

    /**
     * 新增好物推文
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存好物推文
     */
    @RequiresPermissions("poem:goodsarticle:add")
    @Log(title = "好物推文", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GoodsArticle goodsArticle) {
        if (StringUtils.isEmpty(goodsArticle.getTitle())) {
            return AjaxResult.success();
        }
        return toAjax(goodsArticleService.insertGoodsArticle(goodsArticle));
    }

    /**
     * 修改好物推文
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GoodsArticle goodsArticle = goodsArticleService.selectGoodsArticleById(id);
        mmap.put("goodsArticle", goodsArticle);
        return prefix + "/edit";
    }

    @GetMapping("/preview/{id}")
    public String preview(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("articleId", id);
        return prefix + "/preview";
    }

    /**
     * 修改保存好物推文
     */
    @RequiresPermissions("poem:goodsarticle:edit")
    @Log(title = "好物推文", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GoodsArticle goodsArticle) {
        return toAjax(goodsArticleService.updateGoodsArticle(goodsArticle));
    }

    /**
     * 删除好物推文
     */
    @RequiresPermissions("poem:goodsarticle:remove")
    @Log(title = "好物推文", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(goodsArticleService.deleteGoodsArticleByIds(ids));
    }

    @RequiresPermissions("poem:goodsarticle:edit")
    @Log(title = "好物推文", businessType = BusinessType.GRANT)
    @PostMapping("/updateStatus/{id}")
    @ResponseBody
    public AjaxResult updateStatus(@PathVariable("id") Long id, Integer status) {

        GoodsArticle goodsArticle = goodsArticleService.selectGoodsArticleById(id);
        goodsArticle.setStatus(status);

        return toAjax(goodsArticleService.updateGoodsArticle(goodsArticle));
    }
}
